/*=========================================================================*/
/**
 *	@file	ReplaceAnimation.m
  */
/*===========================================================================*/
//==============================================================================
//	IMPORT
//==============================================================================
#import <QuartzCore/QuartzCore.h>
#import "libReplaceAnimation.h"


@implementation ReplaceAnimation
@synthesize delegate = m_delegate;
@synthesize duration = m_duration;

- (id)init
{
	self = [super init];
	if ( self ) {
		
		if ( [self isMemberOfClass:[ReplaceAnimation class]] ) {
			SAFE_RELEASE(self);
		} else {
			m_delegate = nil;
			m_duration = 0.1f;
		}
	}
	return self;
}

- (void)setAnimation:(UIView*)view
{
	[self retain];
}


- (void)finishAnimation
{
	if ( [m_delegate respondsToSelector:@selector(replaceAnimationDidStop:)] ) {
		[m_delegate replaceAnimationDidStop:self];
	}
	[self release];
}


#pragma mark 
+ (ReplaceAnimation*)NullAnimation
{
	return [[[ReplaceNullAnimation alloc] init] autorelease];
}
@end


#pragma mark -
#pragma mark 
@implementation ReplaceNullAnimation
- (void)setAnimation:(UIView *)view
{
	[self performSelector:@selector(finishAnimation)
			   withObject:nil
			   afterDelay:self.duration];
	[super setAnimation:view];
}
@end


#pragma mark -
#pragma mark CATransition
@implementation ReplaceCATransitionAnimation
- (id)initWithType:(NSString*)type subtype:(NSString*)subtype
{
	self = [super init];
	if ( self ) {
		m_type    = [type    copy];
		m_subtype = [subtype copy];
	}
	return self;
}

- (void)dealloc
{
	SAFE_RELEASE(m_type);
	SAFE_RELEASE(m_subtype);
	[super dealloc];
}


- (void)setAnimation:(UIView*)view
{
	CATransition* anim = [CATransition animation];
	[anim setDelegate:self];
	
	[anim setType:m_type];
	[anim setSubtype:m_subtype];
	
	[anim setDuration:self.duration];
	[anim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
	
	[view.layer addAnimation:anim forKey:kCATransition];
	[super setAnimation:view];
}


#pragma mark CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
	[self finishAnimation];
}
@end


@implementation ReplaceAnimation(ReplaceCATransitionAnimation)
+ (ReplaceAnimation*)CAAnimation:(NSString*)type subtype:(NSString*)subtype
{
	return [[[ReplaceCATransitionAnimation alloc] initWithType:type subtype:subtype] autorelease];
}

+ (ReplaceAnimation*)CAPushAnimation:subtype
{
	return [[[ReplaceCATransitionAnimation alloc] initWithType:kCATransitionPush subtype:subtype] autorelease];
}
@end



#pragma mark -
#pragma mark UIView
@implementation ReplaceUIViewAnimation
- (id)initWithType:(UIViewAnimationTransition)type
{
	self = [super init];
	if ( self ) {
		m_type = type;
	}
	return self;
}

- (void)setAnimation:(UIView*)view
{
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(finishAnimation)];
	[UIView setAnimationDuration:self.duration];
	[UIView setAnimationTransition:m_type forView:view cache:YES];
	
	[UIView commitAnimations];
	[super setAnimation:view];
}

@end


@implementation ReplaceAnimation(ReplaceUIViewAnimation)
+ (ReplaceAnimation*)UIViewAnimation:(UIViewAnimationTransition)type
{
	return [[[ReplaceUIViewAnimation alloc] initWithType:type] autorelease];
}
@end
